#!/bin/bash

# 判断是否设置环境变量 $ZBP_BASE_PATH
if [ -z "$ZBP_BASE_PATH" ]; then
    echo "环境变量 ZBP_BASE_PATH 未设置，请先阅读文档 https://alidocs.dingtalk.com/i/nodes/dQPGYqjpJYgo29QesdPED3yLWakx1Z5N?doc_type=wiki_doc "
    exit 1
fi

# 应用安装根目录
INSTALL_DIR="$ZBP_BASE_PATH/zbp/apps"
# 应用文件夹名称
PROJECT_INSTALL_DIR="data-manage"
# 安装包下载地址
PACKAGE_URL="https://gitee.com/iamwx/deploy-files/raw/master/apps/target/apps/data-manage.tar.gz"

echo "1. 初始化安装目录"
mkdir -p $INSTALL_DIR || { echo "无法创建安装目录: $INSTALL_DIR"; exit 1; }

echo "2. wget 校验"
if ! command -v wget &> /dev/null; then
    echo "wget 未安装，正在安装..."
    sudo yum install -y wget || { echo "wget 安装失败"; exit 1; }
fi

echo "3. 判断文件夹是否存在"
if [ -d "$INSTALL_DIR/$PROJECT_INSTALL_DIR" ]; then
    echo "文件夹 $INSTALL_DIR/$PROJECT_INSTALL_DIR 已存在，结束安装"
    exit 1
else
    echo "4. 下载安装包到安装目录"
    cd $INSTALL_DIR || { echo "无法进入安装目录: $INSTALL_DIR"; exit 1; }
    wget $PACKAGE_URL -O $PROJECT_INSTALL_DIR.tar.gz || { echo "下载安装包失败: $PACKAGE_URL";exit 1; }
    if [ $? -eq 0 ]; then
        #
        echo "5. 解压安装包、删除安装包"
        tar -zxvf $PROJECT_INSTALL_DIR.tar.gz || { echo "解压安装包失败: $PROJECT_INSTALL_DIR.tar.gz"; exit 1; }
        rm -rf $PROJECT_INSTALL_DIR.tar.gz

    else
        echo "下载安装包失败: $PACKAGE_URL"
        exit 1
    fi
fi

# 定义红色文字的ANSI转义序列
RED='\033[0;31m'
NC='\033[0m' # 没有颜色（用于重置颜色）
echo -e "${RED}安装目录已初始化完成，继续按照文档说明，下载jar包并进行上传后再进行部署。${NC}"